package com.fenghuajueli.module_user.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.StrikethroughSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.fenghuajueli.libbasecoreui.listener.OnListClickListener;
import com.fenghuajueli.libbasecoreui.data.entity.goods.GoodsEntity;
import com.fenghuajueli.module_user.R;

import java.util.List;

public class VipPriceAdapter extends RecyclerView.Adapter<VipPriceAdapter.ViewHolder> {
    private Context mContext;
    private List<GoodsEntity> goodsEntityList;
    private int currentPosition = 0;
    private SpannableString spannableString;
    private StrikethroughSpan strikethroughSpan;
    private OnListClickListener<GoodsEntity> onItemClick;

    public VipPriceAdapter(Context mContext, List<GoodsEntity> goodsEntityList) {
        this.mContext = mContext;
        this.goodsEntityList = goodsEntityList;
        strikethroughSpan = new StrikethroughSpan();
    }

    @NonNull
    @Override
    public VipPriceAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.vip_price_item_layout, parent, false);
        return new ViewHolder(view);
    }

    public void setOnItemClick(OnListClickListener<GoodsEntity> onItemClick) {
        this.onItemClick = onItemClick;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        GoodsEntity goodsEntity = goodsEntityList.get(position);

        holder.tvVipName.setText(goodsEntity.getGoods_name());
        holder.tvVipName.setSelected(true);

        String originPrice = "原价：" + goodsEntity.getOriginal_price();
        spannableString = new SpannableString(originPrice);
        spannableString.setSpan(strikethroughSpan, 0, originPrice.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        holder.tvOriginPrice.setText(spannableString);

        holder.tvFirstPrice.setText("¥" + goodsEntity.getGoods_price());

        if (goodsEntity.getVip_type() > 100) {
            holder.tvOriginPrice.setVisibility(View.INVISIBLE);
            holder.tvTip.setVisibility(View.VISIBLE);
        } else {
            holder.tvTip.setVisibility(View.INVISIBLE);
            holder.tvOriginPrice.setVisibility(View.VISIBLE);
        }

        if (TextUtils.isEmpty(goodsEntity.getGoods_badge())) {
            holder.ivSelectedTips.setVisibility(View.GONE);
            holder.tvSelectedTips.setVisibility(View.GONE);
        } else {
            holder.ivSelectedTips.setVisibility(View.VISIBLE);
            holder.tvSelectedTips.setVisibility(View.VISIBLE);
        }

        if (currentPosition == position) {
            int color = Color.parseColor("#FFFFFF");
//            holder.tvOriginPrice.setTextColor(color);
            holder.tvTip.setTextColor(color);
            holder.ivBg.setImageResource(R.mipmap.user_img_permanentmember);
        } else {
            int color = Color.parseColor("#909090");
//            holder.tvOriginPrice.setTextColor(color);
            holder.tvTip.setTextColor(color);
            holder.ivBg.setImageResource(R.mipmap.user_img_quartermembership);
        }


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setCurrentPosition(holder.getAbsoluteAdapterPosition());
                if (onItemClick != null) {
                    onItemClick.itemClick(holder.getAbsoluteAdapterPosition(), goodsEntity);
                }
            }
        });
    }


    public int getCurrentPosition() {
        return currentPosition;
    }


    public void setCurrentPosition(int currentPosition) {
        this.currentPosition = currentPosition;
        notifyDataSetChanged();
    }

    public void refreshData(List<GoodsEntity> goodsEntityList) {
        this.goodsEntityList = goodsEntityList;
        notifyDataSetChanged();
    }


    @Override
    public int getItemCount() {
        return goodsEntityList.size();
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {

        public ImageView ivBg;
        public TextView tvVipName;
        public TextView tvFirstPrice;
        public TextView tvOriginPrice;
        public ImageView ivSelectedTips;
        public TextView tvSelectedTips;
        public TextView tvTip;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivBg = itemView.findViewById(R.id.ivBg);
            tvVipName = itemView.findViewById(R.id.tvVipName);
            tvFirstPrice = itemView.findViewById(R.id.tvFirstPrice);
            tvOriginPrice = itemView.findViewById(R.id.tvOriginPrice);
            ivSelectedTips = itemView.findViewById(R.id.ivSelectedTips);
            tvSelectedTips = itemView.findViewById(R.id.tvSelectedTips);
            tvTip = itemView.findViewById(R.id.tvTip);
        }
    }

}
